package com.ruiyi.careryun.web.demo;

import java.io.IOException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruiyi.careryun.beans.common.FileResponse;
import com.ruiyi.careryun.beans.common.ResultMsg;
import com.ruiyi.careryun.beans.user.UserInfo;
import com.ruiyi.careryun.service.demo.DemoService;
import com.ruiyi.careryun.utils.FileUploadUtil;
import com.ruiyi.careryun.utils.ZipUtil;

@Controller
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    private DemoService demoService;

    @RequestMapping("/bootstrapvalidator")
    public String toBootstrapValidator() {
        return "/demo/bootstrapValidator";
    }

    @RequestMapping("/layer")
    public String toLayer() {
        try {
            demoService.addTestTransaction();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/demo/layer";
    }

    @RequestMapping("/angularjs/helloworld")
    public String toHelloWorld() {
        return "/demo/angularjs/helloWorld";
    }

    @RequestMapping("/fileinput")
    public String toFileInput() {
        return "/demo/fileInput";
    }

    @RequestMapping(value = "/fileinput/adduser", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg<Map<String, Object>> addUser(UserInfo user, @RequestParam("doc") MultipartFile file) {
        FileResponse fileResponse = FileUploadUtil.saveImage(file, "E:\\static\\img");
        String targetPath = "E:\\static\\img\\" + fileResponse.getNewFileName();
        try {
            ZipUtil.unZip(targetPath, "E:\\static\\img\\");
            return ResultMsg.success();
        } catch (IOException e) {
            e.printStackTrace();
            return ResultMsg.error("服务器异常");
        }
    }

    @RequestMapping("/preview")
    public String toPreview() {
        return "/demo/preview";
    }

    @RequestMapping("/ueditor")
    public String toUeditor() {
        return "/demo/ueditor";
    }

    @RequestMapping("/testtransaction")
    @ResponseBody
    public Boolean testTransaction() {
        try {
            demoService.addTestTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @RequestMapping("/mongo/add")
    @ResponseBody
    public Boolean testMongoAdd() {
        try {
            demoService.testMongoAdd();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
